package com.syqc.monitor.ui.photo

import autodispose2.autoDispose
import com.syqc.base.BaseReportViewModel
import com.syqc.entity.PhotoInfo
import com.syqc.monitor.R
import com.syqc.net.ServiceHelper
import com.syqc.utils.DataUtil
import com.syqc.utils.TimeUtil
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.subjects.PublishSubject

/**
 *@Author Jasper
 *@Time   2020/12/10
 *@Desc   照片查询
 */
class PhotosViewModel :
    BaseReportViewModel(isThirty = true,isSimpleThirty=true) {
    private val dataSub by lazy { PublishSubject.create<ArrayList<PhotoInfo>>() }
    fun dataObserver(): Observable<ArrayList<PhotoInfo>> {
        return dataSub.hide()
    }

    override fun getData(carId: String, startTime: String, endTime: String) {
        super.getData(carId, startTime, endTime)
        toastLoading(true)
        ServiceHelper.getRepository().getHistoryPhotos(
            carId, TimeUtil.instance.formatHms(startTime),
            TimeUtil.instance.formatEndHms(endTime)
        ).autoDispose(this).subscribe({
            toastLoading(false)
            if (isHasListData(
                    it,
                    R.string.toast_no_photo
                )
            ) return@subscribe
            dataSub.onNext(it)
            DataUtil.mPhotoInfo = it
        }, error)
    }
}
